<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
# Monitoring template.
#
# This defines all the configurable parameters and other objects that are needed to run the EFK service. This defines elasticsearch,
# logstash, kibana and zipkin services that are part of jhipster-monitoring stack. This template can even be saved in OpenShift
# namespace as well so that have the flexibility to do any project specific customizations. Pls note wherever
# displayName says *** PLEASE DO NOT CHANGE THIS ***, don't touch that as those parameters will be referenced in other places.
#
apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
kind: Template
metadata:
  name: jhipster-monitoring-template
  namespace: <%= openshiftNamespace %>
  annotations:
    description: This template defines objects that are required to spin up monitoring pod
    tags: elasticsearch, logstash, kibana, zipkin, jhipster-monitoring<% if (storageType === 'persistent') { %> ,persistent <% } %> <% if (storageType === 'ephemeral') { %> ,ephemeral <% } %>
    openshift.io/display-name: jhipster-monitoring-template
    openshift.io/long-description: >-
        This template provides objects that are required to spin up a monitoring pod. It contains
        elasticsearch for storing logs, logstash for accumulating logs to elasticsearch, kibana for visualization and zipkin for tracing (for
        microservices).<% if (storageType =='persistent') { %>The database is stored on persistent storage, so any restart of the service will
        not cause any impact to the data. Please make sure you have provisioned PVs (Persistent Volumes) before using this template. <% } %>
        <% if (storageType === 'ephemeral') { %>The database is not stored on persistent storage, so any restart of the service will result in all data being lost.<% } %>
    openshift.io/provider-display-name: JHipster-OpenShift
labels:
  app: jhipster-monitoring
  createdBy: JHipster-Team
parameters:
  -
    name: ES_APP_NAME
    value: jhipster-elasticsearch
    description: Name of the elastisearch application
    required: true
    displayName: Elastic Search Application Name
  -
    name: LS_APP_NAME
    value: jhipster-logstash
    description: Name of the logstash application
    required: true
    displayName: Logstash Application Name
  -
    name: KB_APP_NAME
    value: jhipster-console
    description: Name of the kibana application
    required: true
    displayName: Kibana Application Name
  -
    name: KB_DASHBOARD_APP_NAME
    value: jhipster-import-dashboards
    description: Name of the preloaded kibana application dashboard
    required: true
    displayName: Kibana Preloaded Application Dashboard Name
<%_ if (deploymentApplicationType === 'microservice') { _%>
  -
    name: ZK_APP_NAME
    value: jhipster-zipkin
    description: Name of the zipkin application
    required: true
    displayName: Zipkin Application Name
<%_ } _%>
  -
    name: SVC_ID
    value: jhipster
    description: Name of the service account
    required: true
    displayName: "*** PLEASE DO NOT CHANGE THIS ***"
  -
    name: VOLUME_CAPACITY
    displayName: Volume Capacity
    description: Volume space available for data, e.g. 512Mi, 2Gi.
    value: 1Gi
    required: true
objects:
<%_ if (storageType === 'persistent') { _%>
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: PersistentVolumeClaim
    metadata:
      name: ${ES_APP_NAME}
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: "${VOLUME_CAPACITY}"
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: PersistentVolumeClaim
    metadata:
      name: ${LS_APP_NAME}
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: "${VOLUME_CAPACITY}"
<%_ } _%>
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: DeploymentConfig
    metadata:
      name: ${ES_APP_NAME}
      labels:
        app: ${ES_APP_NAME}
    spec:
# This is to define the deployment strategy (either be Rolling or Recreate)
#     strategy:
#       type: Rolling
#       rollingParams:
#         updatePeriodSeconds: 1
#         intervalSeconds: 1
#         timeoutSeconds: 600
#         maxUnavailable: 25%
#         maxSurge: 25%
#       resources:
      triggers:
        -
          type: ConfigChange
      replicas: 1
      selector:
        app: ${ES_APP_NAME}
      template:
        metadata:
          labels:
            app: ${ES_APP_NAME}
        spec:
          volumes:
          - name: ${ES_APP_NAME}-data
            <%_ if (storageType === 'persistent') { _%>
            persistentVolumeClaim:
              claimName: ${ES_APP_NAME}
            <%_ } _%>
            <%_ if (storageType === 'ephemeral') { _%>
            emptyDir: {}
            <%_ } _%>
          containers:
          - name: ${ES_APP_NAME}
            image: <%= DOCKER_JHIPSTER_ELASTICSEARCH %>
            ports:
            - containerPort: 9200
              name: http
              protocol: TCP
            - containerPort: 9300
              name: transport
              protocol: TCP
            volumeMounts:
            - name: ${ES_APP_NAME}-data
              mountPath: /usr/share/elasticsearch/data/
            resources:
            imagePullPolicy: IfNotPresent
          serviceAccount: ${SVC_ID}
          serviceAccountName: ${SVC_ID}
          restartPolicy: Always
          terminationGracePeriodSeconds: 30
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Service
    metadata:
      name: ${ES_APP_NAME}
      labels:
        app: ${ES_APP_NAME}
    spec:
      ports:
        -
          name: http
          port: 9200
          protocol: TCP
          targetPort: 9200
        -
          name: transport
          port: 9300
          protocol: TCP
          targetPort: 9300
      selector:
        app: ${ES_APP_NAME}
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: DeploymentConfig
    metadata:
      name: ${LS_APP_NAME}
      labels:
        app: ${LS_APP_NAME}
    spec:
# This is to define the deployment strategy (either be Rolling or Recreate)
#     strategy:
#       type: Rolling
#       rollingParams:
#         updatePeriodSeconds: 1
#         intervalSeconds: 1
#         timeoutSeconds: 600
#         maxUnavailable: 25%
#         maxSurge: 25%
#       resources:
      triggers:
        -
          type: ConfigChange
      replicas: 1
      selector:
        app: ${LS_APP_NAME}
      template:
        metadata:
          labels:
            app: ${LS_APP_NAME}
        spec:
          volumes:
          - name: ${LS_APP_NAME}-data
            <%_ if (storageType === 'persistent') { _%>
            persistentVolumeClaim:
              claimName: ${LS_APP_NAME}
            <%_ } _%>
            <%_ if (storageType === 'ephemeral') { _%>
            emptyDir: {}
            <%_ } _%>
          containers:
          - name: ${LS_APP_NAME}
            image: <%= DOCKER_JHIPSTER_LOGSTASH %>
            ports:
            - containerPort: 5000
              name: udp
              protocol: UDP
            - containerPort: 5000
              name: tcp
              protocol: TCP
            volumeMounts:
            - name: ${LS_APP_NAME}-data
              mountPath: /usr/share/logstash/data/
            resources:
            imagePullPolicy: IfNotPresent
          serviceAccount: ${SVC_ID}
          serviceAccountName: ${SVC_ID}
          restartPolicy: Always
          terminationGracePeriodSeconds: 30
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Service
    metadata:
      name: ${LS_APP_NAME}
      labels:
        app: ${LS_APP_NAME}
    spec:
      ports:
        - name: udp
          port: 5000
          protocol: UDP
          targetPort: 5000
        - name: tcp
          port: 5000
          protocol: TCP
          targetPort: 5000
      selector:
        app: ${LS_APP_NAME}
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: DeploymentConfig
    metadata:
      name: ${KB_APP_NAME}
      labels:
        app: ${KB_APP_NAME}
    spec:
# This is to define the deployment strategy (either be Rolling or Recreate)
#     strategy:
#       type: Rolling
#       rollingParams:
#         updatePeriodSeconds: 1
#         intervalSeconds: 1
#         timeoutSeconds: 600
#         maxUnavailable: 25%
#         maxSurge: 25%
#       resources:
      triggers:
        -
          type: ConfigChange
      replicas: 1
      selector:
        app: ${KB_APP_NAME}
      template:
        metadata:
          labels:
            app: ${KB_APP_NAME}
        spec:
          containers:
          - name: ${KB_APP_NAME}
            image: <%= DOCKER_JHIPSTER_CONSOLE %>
            ports:
            - containerPort: 5601
              name: http
              protocol: TCP
            resources:
            imagePullPolicy: IfNotPresent
          serviceAccount: ${SVC_ID}
          serviceAccountName: ${SVC_ID}
          restartPolicy: Always
          terminationGracePeriodSeconds: 30
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Service
    metadata:
      name: ${KB_APP_NAME}
      labels:
        app: ${KB_APP_NAME}
    spec:
      ports:
        -
          name: http-bc
          port: 5601
          protocol: TCP
          targetPort: 5601
        -
          name: http
          port: 80
          protocol: TCP
          targetPort: 5601
      selector:
        app: ${KB_APP_NAME}
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>1
    kind: Route
    metadata:
      name: ${KB_APP_NAME}
    spec:
      to:
        kind: Service
        name: ${KB_APP_NAME}
        weight: "100"
      port:
        targetPort: "http"
      wildcardPolicy: None
  -
    apiVersion: <%= KUBERNETES_BATCH_API_VERSION %>
    kind: Job
    metadata:
      name: ${KB_DASHBOARD_APP_NAME}
      labels:
        job: ${KB_DASHBOARD_APP_NAME}
    spec:
      template:
        metadata:
          labels:
            job: ${KB_DASHBOARD_APP_NAME}
        spec:
          containers:
          - name: ${KB_DASHBOARD_APP_NAME}
            image: <%= DOCKER_JHIPSTER_IMPORT_DASHBOARDS %>
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure
<%_ if (deploymentApplicationType === 'microservice') { _%>
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: DeploymentConfig
    metadata:
      name: ${ZK_APP_NAME}
      labels:
        app: ${ZK_APP_NAME}
    spec:
# This is to define the deployment strategy (either be Rolling or Recreate)
#     strategy:
#       type: Rolling
#       rollingParams:
#         updatePeriodSeconds: 1
#         intervalSeconds: 1
#         timeoutSeconds: 600
#         maxUnavailable: 25%
#         maxSurge: 25%
#       resources:
      triggers:
        -
          type: ConfigChange
      replicas: 1
      selector:
        app: ${ZK_APP_NAME}
      template:
        metadata:
          labels:
            app: ${ZK_APP_NAME}
        spec:
          containers:
          - name: ${ZK_APP_NAME}
            image: <%= DOCKER_JHIPSTER_ZIPKIN %>
            ports:
            - containerPort: 9411
              name: http
              protocol: TCP
            env:
            - name: ES_HOSTS
              value: ${ES_APP_NAME}:9200
            - name: ZIPKIN_UI_LOGS_URL
              value: "http://${KB_APP_NAME}/app/kibana#/dashboard/logs-dashboard?_g=(refreshInterval:(display:Off,pause:!f,value:0),time:(from:now-1h,mode:quick,to:now))&_a=(filters:!(),options:(darkTheme:!f),panels:!((col:1,id:logs-levels,panelIndex:2,row:1,size_x:6,size_y:3,type:visualization),(col:7,columns:!(stack_trace),id:Stacktraces,panelIndex:7,row:1,size_x:4,size_y:3,sort:!('@timestamp',desc),type:search),(col:11,id:Log-forwarding-instructions,panelIndex:8,row:1,size_x:2,size_y:3,type:visualization),(col:1,columns:!(app_name,level,message),id:All-logs,panelIndex:9,row:4,size_x:12,size_y:7,sort:!('@timestamp',asc),type:search)),query:(query_string:(analyze_wildcard:!t,query:'{traceId}')),title:logs-dashboard,uiState:())"
            resources:
            imagePullPolicy: IfNotPresent
          serviceAccount: ${SVC_ID}
          serviceAccountName: ${SVC_ID}
          restartPolicy: Always
          terminationGracePeriodSeconds: 30
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Service
    metadata:
      name: ${ZK_APP_NAME}
      labels:
        app: ${ZK_APP_NAME}
    spec:
      ports:
        -
          name: http-bc
          port: 9411
          protocol: TCP
          targetPort: 9411
        -
          name: http
          port: 80
          protocol: TCP
          targetPort: 9411
      selector:
        app: ${ZK_APP_NAME}
  -
    apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
    kind: Route
    metadata:
      name: ${ZK_APP_NAME}
    spec:
      to:
        kind: Service
        name: ${ZK_APP_NAME}
        weight: "100"
      port:
        targetPort: "http"
      wildcardPolicy: None
<%_ } _%>
